// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


// A generic onclick callback function.
function genericOnClick(info, tab) {
	console.log("item " + info.menuItemId + " was clicked");

	//console.log("info: " + JSON.stringify(info));
	//console.log("tab: " + JSON.stringify(tab));
	
	// menuItemId 1 is the first contexMenu has benn created
	if ( info.menuItemId == 2 ){
		console.log("select: \""+info.selectionText+"\"");
		var GoToUrl = "http://www.google.com.tw/dictionary?langpair=en|zh-TW&q="+info.selectionText;
		chrome.tabs.create({"url":GoToUrl,"index":tab.index+1});
	} else if ( info.menuItemId == 1 ){
		console.log("page: \""+info.selectionText+"\"");
		var GoToUrl = "http://www.google.com.tw/dictionary";
		chrome.tabs.create({"url":GoToUrl,"index":tab.index+1});
	}
}



// Create one test item for each context type.
// var contexts = ["page","selection","link","editable","image","video","audio"];
var contexts = ["page","selection"];
for (var i = 0; i < contexts.length; i++) {
	var context = contexts[i];
	var title = "Test '" + context + "' menu item";
	switch (context){
		case "page":
			title = "Google dictionary";
			break;
		case "selection":
			title = "Seach by Google dictionary";
			break;
		default:
			break;
	}

	var id = chrome.contextMenus.create({"title": title, "contexts":[context],"onclick": genericOnClick});
	console.log("'" + context + "' item:" + id);
}


// Create a parent item and two children.
//var parent = chrome.contextMenus.create({"title": "Test parent item"});
//var child1 = chrome.contextMenus.create(
//		{"title": "Child 1", "parentId": parent, "onclick": genericOnClick});
//var child2 = chrome.contextMenus.create(
//		{"title": "Child 2", "parentId": parent, "onclick": genericOnClick});
//console.log("parent:" + parent + " child1:" + child1 + " child2:" + child2);



// Intentionally create an invalid item, to show off error checking in the
// create callback.
//console.log("About to try creating an invalid item - an error about " +
//		"item 999 should show up");
//chrome.contextMenus.create({"title": "Oops", "parentId":999}, function() {
//		if (chrome.extension.lastError) {
//		console.log("Got expected error: " + chrome.extension.lastError.message);
//		}
//		});
